package com.guys8.cemsbackend.utils.UploadUtil;

import com.sun.org.apache.xpath.internal.operations.Mult;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class UploadUtil {

    // 项目根路径下的目录  -- SpringBoot static 目录相当于是根路径下（SpringBoot 默认）
    public final static String IMG_PATH_PREFIX = "static/outsourcin/images";

    public static File getImgDirFile(String path){
        // 构建上传文件的存放 "文件夹" 路径
        String fileDirPath = new String("src/main/resources/" + path);

        File fileDir = new File(fileDirPath);
        if(!fileDir.exists()){
            // 递归生成文件夹
            fileDir.mkdirs();
        }
        return fileDir;
    }

    public static byte[] getnotefile(String notepath) throws IOException {
        File file = new File(notepath);
        byte[] bytesArray = new byte[(int) file.length()];
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            fis.read(bytesArray); //read file into bytes[]
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return bytesArray;
    }

    public static Boolean uploadnotefile(String schoolid, String outid , MultipartFile file){
        File filedir = getImgDirFile("note/"+ schoolid );
        String url = filedir.getAbsolutePath();
        File newFile = new File(url + File.separator + outid+".jpg");
        try {
            file.transferTo(newFile);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void main(String[] args) {

    }
}